<?php

//接收到客户端的响应 返回评论数据

//载入配置文件
require_once '../../functions.php';
//配置分页参数
$size = 20;
$page = empty($_GET['page']) ? 1: $_GET['page'];
//越过多少条取几条
$offset = ($page - 1) * $size;



//获取最大页码
$total_count = xiu_fetch_one('select
  count(1) as count from comments
  inner join posts on comments.post_id = posts.id')['count'];

$total_page = ceil($total_count / $size);
$sql = sprintf('select
  comments.*, posts.title as post_title
from comments
inner join posts on comments.post_id = posts.id
order by comments.created desc
limit %d, %d;', $offset , $size);

//查询数据

$comments = xiu_fetch_all($sql);
//将数据转成字符串的格式(序列化)
$json = json_encode(array('comments' => $comments,'total_page' => $total_page));
header('Content-Type:application/json');
//输出
 
echo $json;